package com.handy.file.chen.jiyu;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

public class File_Process { 
	private static final String TAG ="File_Process";

	public static byte[] get_byte(String filename){
		
		File file = new File(filename);
		byte[] b = new byte[(int) file.length()];
		
		
		
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(b);
			for (int i = 0; i < b.length; i++) {
				System.out.print((char)b[i]);
			}
		} catch (FileNotFoundException e) {
			
			System.out.println("File Not Found.");
			e.printStackTrace(); 
			
		}
		catch (IOException e1){
			System.out.println("Error Reading The File.");
			e1.printStackTrace();
		}
		return b;
	}
	/** tunr file into a byte array into file 
	 * 
	 * @param file_byte source file byte
	 * @return 
	 * @throws IOException 
	 */
	public static boolean byte2file(byte[] file_byte, String filename) throws IOException{
		
		FileOutputStream fileOuputStream = new FileOutputStream(filename);
		
	    fileOuputStream.write(file_byte);
	    
	    fileOuputStream.close();
		
		return true;
	}
	
	/** convert to bitmap 
	 * 
	 * 
	 * @param filename
	 * @return
	 */
	public static byte[] getByteArray(Bitmap bitmap) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 0, bos);
		return bos.toByteArray();
	}
	/**
	 * convert a byte to bitmap
	 * @param bitmap
	 * @return
	 */
	public Bitmap getBitmap(byte[] bitmap) {
		return BitmapFactory.decodeByteArray(bitmap , 0, bitmap.length);
	}
	
	/**
	 * 
	 * @param filename, directory of the file
	 * @return
	 */
	public static byte[] jpeg2byte(String filename){
		Bitmap bmp = BitmapFactory.decodeFile(filename);
		byte[] bos = File_Process.getByteArray(bmp);
		
		return bos;
		
	}
	/**
	 * 
	 * @param source_byte: byte array of the 
	 * @return a bitmap instance 
	 */
	
	public static Bitmap byte2image(byte[] source_byte){
		Bitmap bmp = BitmapFactory.decodeByteArray(source_byte, 0, source_byte.length);		
		return bmp;
				
	}
	/**
	 * print the bitmap in the sdcard
	 * 
	 * @param filename
	 * @throws FileNotFoundException 
	 */
	public static void write_bitmap(String filename, Bitmap bmp) throws FileNotFoundException{
		FileOutputStream out = new FileOutputStream(filename);
		
	    bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
	    	
	}
	
	public static Bitmap  byte2bmp(byte[] image_byte_array, String filename) throws FileNotFoundException{
		Bitmap bmp = byte2image(image_byte_array);
		write_bitmap(filename, bmp);
		return bmp;
	}
	public static long  byte_to_int(byte[] b){
		String str_b = b.toString();
		Log.d(TAG,str_b);
		return Long.parseLong(str_b);
		
		
		
	}
	/**
	 * conver from long to byte array
	 * @param a
	 * @return
	 */
	public static byte[] int2byte(int num){
		byte[] byteNum = new byte[4];
		
		for (int ix=0;ix<4;++ix){
			int offset = 32-(ix+1)*8;
			byteNum [ix] = (byte) ((num >> offset) & 0xff);
			
		}
		return byteNum;
	}
	
}
